﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebUI.Areas.Bots.Models;
using Services;
using DAL;

namespace WebUI.Areas.Bots.Controllers
{
    public partial class UsersAndMachinesController : BaseController
    {
        #region Member Variables

        readonly IBotService _botService;
        readonly IBotsUserRepository _botsUserRepository;
        readonly IBotsMachineRepository _botsMachineRepository;

        #endregion

        #region Constructor

        public UsersAndMachinesController(ISessionHelper sessionHelper, IBotService botService, IBotsUserRepository botsUserRepository, IBotsMachineRepository botsMachineRepository)
            : base(sessionHelper)
        {
            this._botService = botService;
            this._botsUserRepository = botsUserRepository;
            this._botsMachineRepository = botsMachineRepository;
        }

        #endregion

        #region Action Methods

        public virtual ViewResult Index(UsersMachinesSetting usersMachinesSetting)
        {
            usersMachinesSetting.IsAllUsers = _sessionHelper.Bot.AllUsers;
            usersMachinesSetting.IsAllMachines = _sessionHelper.Bot.AllMachines;
            usersMachinesSetting.SelectedUsers = _sessionHelper.Bot.BotsUsers;
            usersMachinesSetting.SelectedMachines = _sessionHelper.Bot.BotsMachines;

            return View(usersMachinesSetting);
        }

        [HttpPost]
        public virtual PartialViewResult AddUser(UsersMachinesSetting usersMachinesSetting, string userName)
        {
            //TODO:Check username and machinename not null
            var user = usersMachinesSetting.SelectedUsers.FirstOrDefault(u => u.UserName == userName);

            if (user != null)
            {
                ModelState.AddModelError("", "User already exists");
            }
            else
            {
                user = new Entities.BotsUser { UserName = userName };
                usersMachinesSetting.SelectedUsers.Add(user);
            }

            return PartialView(MVC.Bots.UsersAndMachines.Views._users, usersMachinesSetting.SelectedUsers);
        }

        public virtual PartialViewResult RemoveUser(UsersMachinesSetting usersMachinesSetting, string userName)
        {
            var user = usersMachinesSetting.SelectedUsers.FirstOrDefault(u => u.UserName == userName);
            usersMachinesSetting.SelectedUsers.Remove(user);

            return PartialView(MVC.Bots.UsersAndMachines.Views._users, usersMachinesSetting.SelectedUsers);
        }

        [HttpPost]
        public virtual PartialViewResult AddMachine(UsersMachinesSetting usersMachinesSetting, string machineName)
        {
            var Machine = usersMachinesSetting.SelectedMachines.FirstOrDefault(u => u.MachineName == machineName);

            if (Machine != null)
            {
                ModelState.AddModelError("", "Machine already exists");
            }
            else
            {
                Machine = new Entities.BotsMachine { MachineName = machineName };
                usersMachinesSetting.SelectedMachines.Add(Machine);
            }

            return PartialView(MVC.Bots.UsersAndMachines.Views._machines, usersMachinesSetting.SelectedMachines);
        }

        public virtual PartialViewResult RemoveMachine(UsersMachinesSetting usersMachinesSetting, string machineName)
        {
            var Machine = usersMachinesSetting.SelectedMachines.FirstOrDefault(u => u.MachineName == machineName);
            usersMachinesSetting.SelectedMachines.Remove(Machine);

            return PartialView(MVC.Bots.UsersAndMachines.Views._machines, usersMachinesSetting.SelectedMachines);
        }

        [HttpPost]
        public virtual ActionResult Save(UsersMachinesSetting usersMachinesSetting, bool IsAllUsers, bool IsAllMachines)
        {
            usersMachinesSetting.IsAllUsers = IsAllUsers;
            usersMachinesSetting.IsAllMachines = IsAllMachines;
            _sessionHelper.Bot.AllUsers = usersMachinesSetting.IsAllUsers;
            _sessionHelper.Bot.AllMachines = usersMachinesSetting.IsAllMachines;

            IDictionary<string, string> validationDictionary = _botService.ValidateUsersMachinesStep(_sessionHelper.Bot);

            foreach (var entry in validationDictionary.Values)
            {
                ModelState.AddModelError(string.Empty, entry);
            }

            if (ModelState.IsValid)
            {
                return RedirectToRoute(new { action = "Index", controller = MVC.Bots.Notifications.Name, step = MVC.Bots.Notifications.Name });
            }
            else
            {
                return View("Index", usersMachinesSetting);
            }
        }

        #endregion
    }
}